<template>
  <div class="container">
    <div class="app-container">
      组织架构
      <el-tree :data="depts" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <!-- <template v-slot="{ node, data }"> -->
        <template v-slot="{ data }">
          <el-row
            style="width: 100%; height: 40px"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="handleCommand($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <add ref="addEdit" :show-dialog.sync="showDialog" :cur-node-id="curNodeId" :is-edit="isEdit" @updata="department" />
  </div>
</template>
<script>
import { department, delDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import Add from './compontents/add.vue'
// import { ArrayToTree } from '@/utils'
export default {
  name: 'Department',
  components: {
    Add
  },
  data() {
    return {
      isEdit: false,
      curNodeId: '',
      showDialog: false,
      depts: [{
        name: '传智教育',
        managerName: '总裁',
        children: [{ name: '总裁办', managerName: '总裁助理' },
          { name: '行政部', managerName: '部长' },
          { name: '财务部', managerName: '会计' }]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.department()
  },
  methods: {
    async department() {
      const res = await department()
      // console.log(res)
      // this.depts = res.data
      this.depts = tranListToTreeData(res.data)
    },
    async handleCommand(type, id) {
      this.curNodeId = id
      // console.log(type)
      this.isEdit = type === 'edit'
      if (type === 'add') {
        this.showDialog = true
        // this.$refs.addEdit.addDetail()
      } else if (type === 'del') {
        // console.log('删除', id)
        this.$confirm('你确定要删除吗?').then(() => {
          this.delDepart(id)
        }).catch(() => {})
      } else if (type === 'edit') {
        this.showDialog = true // 显示弹窗
        this.$nextTick(() => {
          this.$refs.addEdit.getDetail() // 让子组件发请求获取详情数据
        })
      }
    },
    async delDepart(id) {
      await delDepartment(id)
      await this.department()
      this.$message.success('删除成功!')
    }
  }

}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
